package com.syscom.server.qd.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.syscom.safe.wac.dao.WacDao;
import com.syscom.safe.wac.exception.WacServiceException;
import com.syscom.safe.wac.txbean.TxContext;
import com.syscom.server.cache.safeCache.vo.RuleQtaHandleCacheVo;
import com.syscom.server.constant.QDCodeConstant;

public class QuotaDaoImpl extends WacDao implements QuotaDao {
	
	public RuleQtaHandleCacheVo[] queryGlobalQuota(TxContext ctx) throws WacServiceException, SQLException {
		StringBuilder sql = new StringBuilder();
		RuleQtaHandleCacheVo[] vos = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		sql.append("select (b.N_RULE_ID) as N_RULE_ID, (b.VC_WARN_TRIGGER_MK) as VC_WARN_TRIGGER_MK, (b.VC_SUBMIT_TRIGGER_MK) as VC_SUBMIT_TRIGGER_MK, (b.VC_REJECT_TRIGGER_MK) as VC_REJECT_TRIGGER_MK, (b.VC_WARN_TRIGGER_COMPA) as VC_WARN_TRIGGER_COMPA, (b.VC_SUBMIT_TRIGGER_COMPA) as VC_SUBMIT_TRIGGER_COMPA, (b.VC_REJECT_TRIGGER_COMPA) as VC_REJECT_TRIGGER_COMPA, (b.N_WARN_QUOTA) as N_WARN_QUOTA, (b.N_SUBMIT_QUOTA) as N_SUBMIT_QUOTA, (b.N_REJECT_QUOTA) as N_REJECT_QUOTA ");
		sql.append(" from rsk_rule a, rsk_rule_handle b ");
		sql.append(" where a.N_RULE_ID = b.N_RULE_ID ");
		sql.append("and a.VC_USED_MK = ? ");
		sql.append("and a.VC_QUOTA_ACCU_MK = ? ");
		sql.append("and a.VC_BROKER_CTRL_MK = ? ");
		sql.append("and a.VC_BROKER_ACCU_MK = ? ");
		sql.append("and b.VC_CTRL_MK = ? ");
		sql.append("and b.VC_LEVEL_ID = ? ");
		
		try {
			stmt = getPreStmt4NewTx(ctx, "db-service", sql.toString());
			
			int iIdx = 1;
			stmt.setString(iIdx++, QDCodeConstant.YES);
			stmt.setString(iIdx++, QDCodeConstant.YES);
			stmt.setString(iIdx++, QDCodeConstant.YES);
			stmt.setString(iIdx++, QDCodeConstant.YES);
			stmt.setString(iIdx++, QDCodeConstant.YES);
			stmt.setString(iIdx++, QDCodeConstant.LEVEL_BROKER);
			
			rs = stmt.executeQuery();
			
			List<RuleQtaHandleCacheVo> lst = new ArrayList<RuleQtaHandleCacheVo>();
			while(rs.next()) {
				int ruleId = rs.getInt("N_RULE_ID");
				String warnTrigMk = rs.getString("VC_WARN_TRIGGER_MK");
				String subTrigMk = rs.getString("VC_SUBMIT_TRIGGER_MK");
				String rejTrigMk = rs.getString("VC_REJECT_TRIGGER_MK");
				String warnCompa = rs.getString("VC_WARN_TRIGGER_COMPA");
				String subCompa = rs.getString("VC_SUBMIT_TRIGGER_COMPA");
				String rejCompa = rs.getString("VC_REJECT_TRIGGER_COMPA");
				double warnQuota = rs.getDouble("N_WARN_QUOTA");
				double subQuota = rs.getDouble("N_SUBMIT_QUOTA");
				double rejQuota = rs.getDouble("N_REJECT_QUOTA");
				
				RuleQtaHandleCacheVo vo = new RuleQtaHandleCacheVo();
				vo.setRuleId(ruleId);
				vo.setProdId("");
				
				if(QDCodeConstant.YES.equals(warnTrigMk)) {
					vo.setWarnTriggerMk(true);
				} else {
					vo.setWarnTriggerMk(false);
				}
				if(QDCodeConstant.YES.equals(subTrigMk)) {
					vo.setSubTriggerMk(true);
				} else {
					vo.setSubTriggerMk(false);
				}
				if(QDCodeConstant.YES.equals(rejTrigMk)) {
					vo.setRejTriggerMk(true);
				} else {
					vo.setRejTriggerMk(false);
				}
				vo.setWarnTriggerCompa(warnCompa);
				vo.setSubTriggerCompa(subCompa);
				vo.setRejTriggerCompa(rejCompa);
				vo.setWarnQuota(warnQuota);
				vo.setSubQuota(subQuota);
				vo.setRejQuota(rejQuota);
				
				lst.add(vo);
			}
			vos = new RuleQtaHandleCacheVo[lst.size()];
			lst.toArray(vos);
		} finally {
			releasePreStmt4NewTx(ctx, "db-service");
		}
		return vos;
	}

	public RuleQtaHandleCacheVo[] queryZoomQuota(TxContext ctx) throws WacServiceException, SQLException {
		StringBuilder sql = new StringBuilder();
		RuleQtaHandleCacheVo[] vos = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		sql.append("select VC_PROD_ID, N_RULE_ID, N_WARN_QUOTA, N_SUBMIT_QUOTA, N_REJECT_QUOTA");
		sql.append(" from rsk_broker_quota ");
		
		try {
			stmt = getPreStmt4NewTx(ctx, "db-service", sql.toString());
			rs = stmt.executeQuery();
			
			List<RuleQtaHandleCacheVo> lst = new ArrayList<RuleQtaHandleCacheVo>();
			while(rs.next()) {
				String prodId = rs.getString("VC_PROD_ID");
				int ruleId = rs.getInt("N_RULE_ID");
				double warnQuota = rs.getDouble("N_WARN_QUOTA");
				double subQuota = rs.getDouble("N_SUBMIT_QUOTA");
				double rejQuota = rs.getDouble("N_REJECT_QUOTA");
				
				RuleQtaHandleCacheVo vo = new RuleQtaHandleCacheVo();
				vo.setProdId(prodId);
				vo.setRuleId(ruleId);
				vo.setWarnQuota(warnQuota);
				vo.setSubQuota(subQuota);
				vo.setRejQuota(rejQuota);
				lst.add(vo);
			}
			vos = new RuleQtaHandleCacheVo[lst.size()];
			lst.toArray(vos);
		} finally {
			releasePreStmt4NewTx(ctx, "db-service");
		}
		return vos;
	}

	public Map<Integer, Integer> queryQuotaUnit(TxContext ctx) throws WacServiceException, SQLException {
		Map<Integer, Integer> map = new Hashtable<Integer, Integer>();
		StringBuilder sql = new StringBuilder();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		sql.append("select N_RULE_ID, N_QUOTA_UNIT from rsk_rule where VC_USED_MK = ? and VC_BROKER_CTRL_MK = ? and VC_BROKER_ACCU_MK = ?");
		
		try {
			stmt = getPreStmt4NewTx(ctx, "db-service", sql.toString());
			stmt.setString(1, QDCodeConstant.YES);
			stmt.setString(2, QDCodeConstant.YES);
			stmt.setString(3, QDCodeConstant.YES);
			
			rs = stmt.executeQuery();
			
			while(rs.next()) {
				int iRuleId = rs.getInt("N_RULE_ID");
				int iQuotaUnit = rs.getInt("N_QUOTA_UNIT");
				map.put(iRuleId, iQuotaUnit);
			}
		} finally {
			releasePreStmt4NewTx(ctx, "db-service");
		}
		return map;
	}

	public Integer[] queryDispatchRules(TxContext ctx) throws WacServiceException, SQLException {
		ArrayList<Integer> lst = new ArrayList<Integer>();
		StringBuilder sql = new StringBuilder();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		sql.append("select N_RULE_ID from rsk_rule_handle where VC_CTRL_MK = ? and VC_LEVEL_ID = ?");
		
		try {
			stmt = getPreStmt4NewTx(ctx, "db-service", sql.toString());
			stmt.setString(1, QDCodeConstant.YES);
			stmt.setString(2, QDCodeConstant.LEVEL_BROKER);
			
			rs = stmt.executeQuery();
			
			while(rs.next()) {
				int iRuleId = rs.getInt("N_RULE_ID");
				lst.add(iRuleId);
			}
		} finally {
			releasePreStmt4NewTx(ctx, "db-service");
		}
		Integer[] iRuleIds = new Integer[lst.size()];
		lst.toArray(iRuleIds);
		return iRuleIds;
	}

	public String[] queryAllProdId(TxContext ctx) throws WacServiceException, SQLException {
		ArrayList<String> lst = new ArrayList<String>();
		StringBuilder sql = new StringBuilder();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		sql.append("select VC_PROD_ID from rsk_prod_prop");
		
		try {
			stmt = getPreStmt4NewTx(ctx, "db-service", sql.toString());
			
			rs = stmt.executeQuery();
			
			while(rs.next()) {
				String sProdId = rs.getString("VC_PROD_ID");
				lst.add(sProdId);
			}
		} finally {
			releasePreStmt4NewTx(ctx, "db-service");
		}
		
		String[] sProdIds = new String[lst.size()];
		lst.toArray(sProdIds);
		
		return sProdIds;
	}

	public List<Integer> queryRuleIds(TxContext ctx, boolean bIsLockProd) throws WacServiceException, SQLException {
		ArrayList<Integer> lst = new ArrayList<Integer>();
		StringBuilder sql = new StringBuilder();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		sql.append("select N_RULE_ID from rsk_rule where VC_BROKER_CTRL_MK = ? and VC_LEVEL_PROD_CTRL_MK = ?");
		
		try {
			stmt = getPreStmt4NewTx(ctx, "db-service", sql.toString());
			stmt.setString(1, QDCodeConstant.YES);
			if(bIsLockProd) {
				stmt.setString(2, QDCodeConstant.YES);
			} else {
				stmt.setString(2, QDCodeConstant.NO);
			}
			
			rs = stmt.executeQuery();
			
			while(rs.next()) {
				int iRuleId = rs.getInt("N_RULE_ID");
				lst.add(iRuleId);
			}
		} finally {
			releasePreStmt4NewTx(ctx, "db-service");
		}
		return lst;
	}
}
